package org.jeecg.modules.lab.items.ibasis.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.items.entity.LabItemsBasisIndex;
import org.jeecg.modules.lab.items.entity.LabItemsBasisItems;
import org.jeecg.modules.lab.items.ibasis.mapper.LabItemsBasisItemsMapper;
import org.jeecg.modules.lab.items.ibasis.mapper.LabItemsBasisMapper;
import org.jeecg.modules.lab.items.ibasis.service.ILabItemsBasisItemsService;
import org.jeecg.modules.lab.items.item.mapper.LabItemsBasisIndexMapper;
import org.jeecg.modules.lab.items.vo.LabItemsBasisItemsVO;
import org.jeecg.modules.lab.items.vo.LabItemsBasisItemsVoPage;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 企业标准下的实验项目清单
 * @Author: jeecg-boot
 * @Date: 2024-05-08
 * @Version: V1.0
 */
@Service
public class LabItemsBasisItemsServiceImpl extends ServiceImpl<LabItemsBasisItemsMapper, LabItemsBasisItems> implements ILabItemsBasisItemsService {

    @Autowired
    private LabItemsBasisItemsMapper labItemsBasisItemsMapper;

    @Autowired
    private LabItemsBasisIndexMapper labItemsBasisIndexMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBasisItems(@NotNull LabItemsBasisItemsVoPage labItemsBasisItemsVoPage) {
        List<LabItemsBasisItemsVO> labItemsBasisItemsVOList = labItemsBasisItemsVoPage.getLabItemsBasisItemsVoList();
        if (labItemsBasisItemsVOList != null) {
            //todo  先删除
            String mainBasisId = labItemsBasisItemsVOList.get(0).getMainBasisId();
            QueryWrapper<LabItemsBasisItems> basisItemsWrapper = new QueryWrapper<>();
            basisItemsWrapper.eq("main_basis_id",mainBasisId);
            labItemsBasisItemsMapper.delete(basisItemsWrapper);
            QueryWrapper<LabItemsBasisIndex> basisIndexWrapper = new QueryWrapper<>();
            basisIndexWrapper.eq("main_basis_id",mainBasisId);
            labItemsBasisIndexMapper.delete(basisIndexWrapper);

            for (LabItemsBasisItemsVO basisItemsVO : labItemsBasisItemsVOList) {
                LabItemsBasisItems basisItems = new LabItemsBasisItems();
                BeanUtils.copyProperties(basisItemsVO, basisItems);
                basisItems.setId(null);
                labItemsBasisItemsMapper.insert(basisItems);
                List<LabItemsBasisIndex> labItemsBasisIndexList = basisItemsVO.getLabItemsBasisIndexList();
                for (LabItemsBasisIndex itemsBasisIndex:labItemsBasisIndexList){
                    itemsBasisIndex.setBasisItemsId(basisItems.getId());
                    itemsBasisIndex.setSynFlag("0");
                    labItemsBasisIndexMapper.insert(itemsBasisIndex);
                }
            }
        }

    }

    @Override
    public LabItemsBasisItemsVoPage getItemsBasisItemsVoPage(String mainBasisId) {

        LabItemsBasisItemsVoPage labItemsBasisItemsVoPage = new LabItemsBasisItemsVoPage();
        QueryWrapper<LabItemsBasisItems> basisItemsWrapper = new QueryWrapper<>();
        basisItemsWrapper.eq("main_basis_id",mainBasisId);
        List<LabItemsBasisItems> labItemsBasisItemsList = labItemsBasisItemsMapper.selectList(basisItemsWrapper);
        List<LabItemsBasisItemsVO> labItemsBasisItemsVOS = new ArrayList<>();
        QueryWrapper<LabItemsBasisIndex> basisIndexWrapper = new QueryWrapper<>();
        for(LabItemsBasisItems itemsBasisItems:labItemsBasisItemsList){
            LabItemsBasisItemsVO labItemsBasisItemsVO = new LabItemsBasisItemsVO();
            BeanUtils.copyProperties(itemsBasisItems, labItemsBasisItemsVO);
            basisIndexWrapper.clear();
            basisIndexWrapper.eq("main_basis_id",mainBasisId);
            basisIndexWrapper.eq("basis_items_id",itemsBasisItems.getId());
            labItemsBasisItemsVO.setLabItemsBasisIndexList(labItemsBasisIndexMapper.selectList(basisIndexWrapper));
            labItemsBasisItemsVOS.add(labItemsBasisItemsVO);
        }
        labItemsBasisItemsVoPage.setLabItemsBasisItemsVoList(labItemsBasisItemsVOS);
        return labItemsBasisItemsVoPage;
    }
}
